from flask import Blueprint, request, jsonify,current_app
import logging, os, json
from utils import common
invite_bp = Blueprint('invite', __name__, url_prefix='/invite')


@invite_bp.route('/code', methods=['POST'])
def invite_code():
    params = request.json
    data =  current_app.config['DB'].get_invite_code(params)

    keys = ['hits', 'reg_num', 'code']
    rv = dict()
    for item in data:
        rv.update(
            {key:value for key,value in zip(keys, item)}
        )
    return jsonify({'code':0, 'data':rv})


@invite_bp.route('/list', methods=['POST'])
def invite_list():
    params = request.json
    data =  current_app.config['DB'].get_invite_list(params)

    keys = ['reward', 'created_at']
    rv = list()
    for item in data:
        rv.append(
            {
                'reward': json.loads(item[0]),
                'created_at':item[1]
            }
        )
    return jsonify({'code':0, 'items':rv, 'total':len(rv), 'page': params.get('page'), 'page_size':params.get('page_sizze')})

@invite_bp.record
def log(app):
    logging.info('register invite blueprint')
